<?php
// +----------------------------------------------------------------------
// |订单模型
// +----------------------------------------------------------------------
// |Data:2016-06-27 下午2:17
// +----------------------------------------------------------------------
// |Author: 吕志雄 <870923001@qq.com>
// +----------------------------------------------------------------------
namespace Wap\Model;
use Common\Model\CommonModel;

class OrderModel extends CommonModel{
    protected $tableName = 'order';

    /**
     * 获取用户登录列表
     * @param $map
     * @param int $limit
     * @return bool
     */
    public function getUserOrderList($map,$limit=10){
        if(empty($map) || !is_array($map)) return false;
        $list = $this->where($map)->field('id,payway,is_comment,shop_id,create_time,status,paytotal')->order('create_time desc')->findPage($limit);
        return $list;
    }

    /**
     * 获取订单扩展列表
     * @param $order_id
     * @return bool|mixed
     */
    public function getOrderExtendList($order_id){
        if(empty($order_id)) return false;
        $list = M('order_extend')->where('order_id='.$order_id)->select();
        return $list;
    }

    /**
     * 获取订单ID
     * @param $orderno
     * @return bool|mixed
     */
    public function getOrderIdByOrderno($orderno){
        if(empty($orderno)) return false;
        return $this->where('orderno='.$orderno)->getField('id');
    }

    /**
     * 数组处理
     * @param $array
     * @return array|bool
     */
    private function dealData($array){
        if(empty($array)) return false;
        $data = array();
        foreach($array as $key=>$val){
            $flag = 2;
            if($data){
                foreach($data as $k=>$v){
                    if($val['attr_key']){
                        if($v['attr_key'] == $val['attr_key']){
                            $data[$k]['num'] ++;
                            $flag = 1;
                            break;
                        }
                    }else{
                     if($v['product_id'] == $val['product_id']){
                         $data[$k]['num'] ++;
                         $flag = 1;
                         break;
                     }
                    }
                }
            }
            if($flag == 2){
                array_push($data,$val);
            }
        }
        return $data;
    }

    /**
     * 构建订单
     * @param $carData 购物车数据
     * @param $data 提交的数据
     * @param $uid 用户uid
     * @param $alltotal 实际金额
     * @param $payway 支付方式
     * @param string $coupons_id 优惠券ID
     * @param string $cprice 优惠金额
     * @return bool
     */
    public function buildOrder($carData,$data,$uid,$alltotal,$payway=2,$coupons_id='',$cprice= ''){
        if(empty($carData) || empty($data) || empty($uid) || empty($alltotal)) return false;
        $carData = $this->dealData($carData);

        $orderno = getOrderNo();
        $addressDetail = D('Wap/Address')->detail($data['address_id']);
        M()->startTrans();
        $arr = array(
            'orderno'=>$orderno,
            'shop_id'=>$data['shop_id'],
            'uid'=>$uid,
            'buyer'=>$addressDetail['contact'],
            'create_time'=>time(),
            'total'=>$data['price'],
            'alltotal'=>$alltotal,
            'status'=>1,
            'address_id'=>$data['address_id'],
            'payway'=>$payway,
            'paytotal'=>$alltotal,
            'deliver_type'=>$data['type'],
            'deliver_price'=>$data['deliver_price'],
            'remark'=>$data['remark'],
            'mobile'=>$addressDetail['mobile'],
            'address'=>$addressDetail['main_address'].' '.$addressDetail['sub_address']
        );
        if($coupons_id){
            $arr['coupons_id'] = $coupons_id;
            $arr['coupons_price'] = $cprice;
        }

        $result = $this->add($arr);
        $flag = 1;
        foreach($carData as $val){
            $detail = D('Admin/Product')->detail($val['product_id']);
            if($val['attr_key']){
                $attrArr = unserialize($detail['attr']);
                foreach($attrArr as $v){
                    if($v['key'] == $val['attr_key']){
                        $unitprice = $v['price'];
                    }
                }
            }else{
                $unitprice = $detail['price'];
            }

            $temp = array(
                'order_id'=>$result,
                'product_id'=>$val['product_id'],
                'product_title'=>$detail['title'],
                'product_img'=>$detail['pic'],
                'product_cid'=>$val['cate_id'],
                'unitprice'=>$unitprice,
                'num'=>$val['num'],
                'attr'=>$val['attr_key']
            );
            $redisArr = array(
                'product_id'=>$val['product_id'],
                'num'=>$val['num']
            );
            $sData = S('ps_product_store_list');
            if($sData){
                array_push($sData,serialize($redisArr));
                S('ps_product_store_list',$sData);
            }else{
                S('ps_product_store_list',array(serialize($redisArr)));
            }

            $res_2 = M('order_extend')->add($temp);
            $res_3 = M('project')->where('id='.$val['product_id'])->setDec('store',$val['num']);
            $res_4 = M('project')->where('id='.$val['product_id'])->setInc('sales_num',$val['num']);
            $res_5 = M('shop')->where('id='.$data['shop_id'])->setInc('sales_nums',$val['num']);
            if(!$res_2 || !$res_3 || !$res_4 || !$res_5){
                $flag = 2;
                M()->rollback();
                return false;
            }
        }
        if($coupons_id){
            $res_6 = M('coupons_receive_logs')->where('coupons_id='.$coupons_id.' and uid='.$uid)->setField('is_use',1);
        }else{
            $res_6 = true;
        }

        $res_7 = M('user_address')->where('id='.$data['address_id'])->setField('use_time',time());
        if($result && $flag == 1 && $res_6 && $res_7){
            M()->commit();
            D('Wap/Address')->clearCache($data['address_id']);
            return $result;
        }else{
            M()->rollback();
            return false;
        }
    }
}
?>